package com.zhizuobiao.class3.controller;

import com.zhizuobiao.class3.entity.Items;
import com.zhizuobiao.class3.exception.MyException;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;

/**
 * 注解配置处理器
 */

@Controller
public class AnnoController {

    // 设置访问地址
    @RequestMapping("/anno")
    public ModelAndView anno() {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("hello");
        mv.addObject("message", "hello anno");
        return mv;
    }


    @RequestMapping("/anno2")
    public ModelAndView anno2(Date now) {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("hello");
        mv.addObject("message", "hello converter = " + now);
        return mv;
    }

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        // 注册date类型，转换
        binder.registerCustomEditor(
                Date.class,
                new CustomDateEditor(
                        new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"), false));
    }


    @RequestMapping("/anno3")
    public ModelAndView anno3(String username, String email, int age) {

        ModelAndView mv = new ModelAndView();
        mv.setViewName("hello");
        mv.addObject("message",
                username + "_" + email + "_" + age);
        return mv;
    }

    @RequestMapping("/anno4")
    public ModelAndView anno4(Items items) {

        ModelAndView mv = new ModelAndView();
        mv.setViewName("hello");
        mv.addObject("message",
                items);
        return mv;
    }

    // items 集合属性赋值
    @RequestMapping("/anno5")
    public ModelAndView anno5(Items items) {

        ModelAndView mv = new ModelAndView();
        mv.setViewName("hello");
        mv.addObject("message",
                items);
        return mv;
    }

    @RequestMapping("/anno6")
    public ModelAndView anno6(String[] hobby) {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("params");
        mv.addObject("data",
                Arrays.toString(hobby));
        return mv;
    }

    @RequestMapping("/anno7")
    public ModelAndView anno7(@RequestParam(value = "name", required = false) String username) {

        ModelAndView mv = new ModelAndView();
        mv.setViewName("params");
        mv.addObject("data",
                "test5 = " + username);
        return mv;
    }


    /**
     * 转发实现
     */
    @RequestMapping("/anno8")
    public String anno8() {

        // 转发字符串前缀
        // "forward:" + 组件访问地址
        return "forward:" + "/WEB-INF/jsp/hello.jsp";
    }

    /**
     * 重定向实现
     */
    @RequestMapping("/anno9")
    public String anno9(RedirectAttributes attributes) {
        attributes.addAttribute("source", "zzb");

        // 重定向字符前缀
        // "redirect:" + URL
//        return "redirect:" + "http://www.baidu.com?source=zzb";
        return "redirect:" + "http://www.baidu.com";
    }

    /**
     * 统一异常处理
     */
    @RequestMapping("/anno10")
    public String anno10() {

        if (true) {
            throw new MyException("测试自定义异常");
        }
        return "redirect:" + "http://www.baidu.com";
    }


    @RequestMapping("/anno11")
    @ResponseBody
    public String anno11() {
        return "hello string";
    }

    /**
     * 返回json数据
     *
     * @return
     */
    @RequestMapping("/anno12")
    @ResponseBody
    public Items anno12() {
        Items items = new Items();
        items.setName("ipad");
        items.setPrice(4999);
        items.setDetail("mac ipad");
        return items;
    }


    /**
     * jsonp 测试
     *
     * @param callback
     * @return
     */
    @RequestMapping("/jsonp")
    @ResponseBody
    public String jsonp(String callback) {
//        return "{'name':'james','age':'23'}";
        String content = "[\"customername1\",\"customername2\"]";

//        System.out.println("--> " + callback + content);
        String result = "{'message':'hello jsonp nihao haha'}";


        return callback + "(" + result + ")";
    }


}
